package com.suos.core.logic;

import com.suos.conf.SuOsProperties;
import com.suos.core.entity.Abnormal;
import com.suos.core.entity.SysInfo;
import com.suos.core.utils.PriUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 处理服务端发来的信息
 *
 * @author : MisShi
 * @date: 2021-10-22 15:57:20
 */
@Slf4j
@NoArgsConstructor
@ChannelHandler.Sharable
public class SugarClientHandler extends ChannelInboundHandlerAdapter {


    private SuOsProperties suOsProperties;

    private SysInfo sysInfo;


    public SugarClientHandler(SuOsProperties suOsProperties, SysInfo sysInfo) {
        this.suOsProperties = suOsProperties;
        this.sysInfo = sysInfo;
    }

    /**
     * order : 1
     *
     * @return boolean
     */
    @Override
    public boolean isSharable() {
        return super.isSharable();
    }

    /**
     * order : 2
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * order : 3
     */
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        log.info(sysInfo.getUnbid() + " 启动");
    }

    /**
     * order : 4
     * <p>
     * 数据传输(存放数据)
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(sysInfo);
    }

    /**
     * order : 5
     * 读取服务端数据
     */
    @SneakyThrows
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof List) {
            PriUtil.sysInfos = (List<SysInfo>) msg;
        } else if (msg instanceof Abnormal) {
            Abnormal abnormal = (Abnormal) msg;
            throw new Exception(abnormal.getErrorMsg());
        }

    }

    /**
     * order : 6
     * 数据读取完毕
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * order : 7
     * 出现异常
     */

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error(cause.getStackTrace()[0] + " " + cause.getMessage());
        ctx.channel().closeFuture();
    }

    /**
     * order : 8
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        ctx.channel().eventLoop().schedule(() -> {
            log.info(sysInfo.getUnbid() + "-->不活跃进行重试");
            try {
                new SugarClient(suOsProperties, sysInfo).start();
            } catch (Exception e) {
                log.warn("{}不活跃重试异常:{}", sysInfo.getUnbid(), e.getStackTrace()[0]);
            }
        }, suOsProperties.getReconInter(), TimeUnit.SECONDS);

    }

    /**
     * order : 9
     */
    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {


    }

    /**
     * order : 10
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    }


    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    }


    @Override
    protected void ensureNotSharable() {
    }
}
